//
//  BLPlayControlViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/6/11.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit

class BLPlayControlViewController: UIViewController {
    
    var roomID : String = ""
    var UrlStr : String = ""
    
    //人气label
    let fansLabel = BLLabel()
    //房间IDlabel
    let roomidLabel = BLLabel()
    //暂停按钮
    var pauseBtn = UIButton()
    //全屏按钮
    var fullBtn = UIButton()
    //全屏按钮
    var backBtn = UIButton()
    
    //单利对象
    var displayerVC : BLPlayerViewControllerTool = BLPlayerViewControllerTool.shareInstance
    var displayerView : UIView?

    
    override func viewDidLoad() {
        super.viewDidLoad()
        dealNotification()
        setupUI()

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        displayerView  = displayerVC.view
        self.addChildViewController(displayerVC)
        view.insertSubview(displayerView!, atIndex: 0)
        displayerView?.snp_makeConstraints(closure: { (make) in
            make.top.right.left.bottom.equalToSuperview()
        })

        if UrlStr != "" {
            displayerVC.setPlayItem(UrlStr)
        }else{
            getRoomData()
        }
    }
    
  }

// MARK: - 视频播放控制UI
extension BLPlayControlViewController{
    
    func setupUI() {
        setupPauseBtn()
        setupBackBtn()
        setupFullBtn()
    }
    
    //设置暂停按钮
    func setupPauseBtn() {
        //暂停按钮
        pauseBtn = UIButton(type: .Custom)
        pauseBtn.sizeToFit()
        pauseBtn.setBackgroundImage(UIImage(named: "btn_v_pause_36x36_"), forState: UIControlState.Normal)
        pauseBtn.setBackgroundImage(UIImage(named: "btn_v_pause_h_36x36_"), forState: UIControlState.Highlighted)
        pauseBtn.addTarget(self, action: #selector(pauseBtnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(pauseBtn)
        pauseBtn.snp_makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-10)
            make.left.equalToSuperview().offset(10)
            make.width.height.equalTo(BL_BtnWH)
        }
    }
    
    //设置全屏按钮
    func setupFullBtn() {
        //全屏按钮
        fullBtn = UIButton(type: .Custom)
        fullBtn.sizeToFit()
        fullBtn.setBackgroundImage(UIImage(named: "btn_vdo_full_36x36_"), forState: UIControlState.Normal)
        fullBtn.setBackgroundImage(UIImage(named: "btn_vdo_full_click_36x36_"), forState: UIControlState.Highlighted)
        fullBtn.addTarget(self, action: #selector(fullBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(fullBtn)
        fullBtn.snp_makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-60)
            make.right.equalToSuperview().offset(-10)
            make.width.height.equalTo(BL_BtnWH)
        }
    }
    
    
    //设置返回上级按钮
    func setupBackBtn() {
        //返回上级按钮
        backBtn = UIButton(type: .Custom)
        backBtn.sizeToFit()
        backBtn.setBackgroundImage(UIImage(named: "btn_round_back_32x32_"), forState: UIControlState.Normal)
        backBtn.setBackgroundImage(UIImage(named: "btn_round_back_h_32x32_"), forState: UIControlState.Highlighted)
        backBtn.addTarget(self, action: #selector(backBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(backBtn)
        backBtn.snp_makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(10)
            make.width.height.equalTo(BL_BtnWH)
        }
    }
}


// MARK: - 播放控制事件
extension BLPlayControlViewController{
    //暂停/播放视频
    func pauseBtnClick(sender: UIButton) {
        
        if displayerVC.isPlay {
            displayerVC.player?.pause()
            pauseBtn.setBackgroundImage(UIImage(named: "btn_v_play_36x36_"), forState: UIControlState.Normal)
            pauseBtn.setBackgroundImage(UIImage(named: "btn_v_play_h_36x36_"), forState: UIControlState.Highlighted)
        }
        else{
            displayerVC.player?.play()
            pauseBtn.setBackgroundImage(UIImage(named: "btn_v_pause_36x36_"), forState: UIControlState.Normal)
            pauseBtn.setBackgroundImage(UIImage(named: "btn_v_pause_h_36x36_"), forState: UIControlState.Highlighted)
        }
        displayerVC.isPlay = !displayerVC.isPlay
        
    }
    //切换到全屏直播
    func fullBtnClick() {
        let fullVC = BLFullPlayerViewController()
        fullVC.roomID = roomID
        self.presentViewController(fullVC, animated: true, completion: nil)
    }
    
    //返回上级
    func backBtnClick() {
        navigationController?.navigationBarHidden = false
        navigationController?.popViewControllerAnimated(true)
    }
    
}


// MARK: - 通知事件处理
extension BLPlayControlViewController : UIAlertViewDelegate {
    /// 处理通知
    func dealNotification() {
        // 切换到移动网络
        NSNotificationCenter.defaultCenter().addObserverForName(kNetworkStausChangeToWWAN, object: nil, queue: NSOperationQueue.currentQueue()) { (NSNotification) in
            
            let view = UIAlertView(title: "当前切换到移动网络",
                                   message: "是否继续播放",
                                   delegate: self,
                                   cancelButtonTitle: "否",
                                   otherButtonTitles: "是")
            
            view.show()
        }
        
        // 切换到wifi网络
        NSNotificationCenter.defaultCenter().addObserverForName(kNetworkStausChangeToWiFi, object: nil, queue: NSOperationQueue.mainQueue()) { (NSNotification) in
            
            isAllowPlay = true
            self.getRoomData()

        }
        
    }
    
    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        // 否就暂停播放
        if buttonIndex == 0 {
            isAllowPlay = false
            self.pauseBtnClick(UIButton())
        }
        // 是就继续播放
        else if buttonIndex == 1 {
            isAllowPlay = true
            self.getRoomData()
        }
    }
    
}

/// MARK: - 网络请求
extension BLPlayControlViewController{
    
    func getRoomData() {
        
        if roomID != "" {
            BLGetDouyuData.shareInstance.getRoomData(roomID: roomID) { (dataArray) in
                self.displayerVC.setPlayItem(dataArray.autoLiveUrl!)
                self.UrlStr = dataArray.autoLiveUrl!
            }
        }
        else if UrlStr != "" {
            self.displayerVC.setPlayItem(UrlStr)
        }
        print(roomID)
    }
}
